package com.tiancheng.trade.commom.web.config;

import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * @Author: kellen
 * @Description: 数据源关闭
 * @Date: create in 2024/11/27 15:15
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class DataSourceShutdownHook implements DisposableBean {
    private final DataSource dataSource;


    @Override
    public void destroy() throws Exception {
        if(dataSource instanceof HikariDataSource){
            log.info("DataSourceShutdownHook bean销毁......");
            ((HikariDataSource) dataSource).close();
        }
    }
}
